package com.tubeonfire.model.admin;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import javax.cache.Cache;
import javax.cache.CacheException;
import javax.cache.CacheManager;

import com.googlecode.objectify.Key;
import com.googlecode.objectify.Objectify;
import com.googlecode.objectify.ObjectifyOpts;
import com.googlecode.objectify.ObjectifyService;
import com.googlecode.objectify.Query;
import com.tubeonfire.entity.Playlist;
import com.tubeonfire.entity.Tag;

public class TagModel {
	private static Objectify ofy;

	private static Cache cache = null;

	private static boolean isRegisted = false;

	private static ObjectifyOpts opts = null;

	private static String cachePrefix = "TagModel_";

	private int limit = 30;

	private int page = 1;

	private int totalResult = 0;

	private int totalPage = 1;

	private List<Tag> listResult = new ArrayList<Tag>();

	public int getTotalPage() {
		totalPage = totalResult / limit;
		if ((totalResult % limit) > 0) {
			totalPage += 1;
		}
		return totalPage;
	}

	public void setTotalPage(int totalPage) {
		this.totalPage = totalPage;
	}

	public int getLimit() {
		return limit;
	}

	public void setLimit(int limit) {
		this.limit = limit;
	}

	public int getPage() {
		return page;
	}

	public void setPage(int page) {
		this.page = page;
	}

	public int getTotalResult() {
		return totalResult;
	}

	public void setTotalResult(int totalResult) {
		this.totalResult = totalResult;
	}

	public List<Tag> getListResult() {
		return listResult;
	}

	public void setListResult(List<Tag> listResult) {
		this.listResult = listResult;
	}

	public static void init() {
		if (!isRegisted) {
			isRegisted = true;
			try {
				ObjectifyService.register(Playlist.class);			
			} catch (Exception e) {
				isRegisted = false;
			}
			try {
				cache = CacheManager.getInstance().getCacheFactory()
						.createCache(Collections.emptyMap());		
			} catch (CacheException e) {
				isRegisted = false;
			}
			opts = new ObjectifyOpts().setSessionCache(true);	
		}
		ofy = ObjectifyService.begin(opts);
	}

	public TagModel() {
		init();
	}

	@SuppressWarnings("unchecked")
	public static void insert(Tag obj) {
		init();
		if (cache != null) {
			String prefix = cachePrefix + "alias_" + obj.getAlias();
			cache.put(prefix, obj);
			System.out.println("Put tag to cache success !");
		}
		ofy.put(obj);
	}

	public static void delete(Tag obj) {
		init();
		String prefix = cachePrefix + "alias_" + obj.getAlias();
		if (cache != null && cache.containsKey(prefix)) {
			cache.remove(prefix);
			System.out.println("Remove tag from cache success !");
		}
		ofy.delete(obj);
	}

	@SuppressWarnings("unchecked")
	public static Tag getByAlias(String alias) {
		init();
		Tag obj = new Tag();
		String prefix = cachePrefix + "alias_" + alias;
		if (cache != null && cache.containsKey(prefix)) {
			obj = (Tag) cache.get(prefix);
			System.out.println("Get tag from cache !");
		} else {
			try {
				obj = ofy.get(new Key<Tag>(Tag.class, alias));
				cache.put(prefix, obj);
				System.out.println("Put tag to cache success !");
			} catch (Exception e) {
				obj = null;
			}
		}
		return obj;
	}

	@SuppressWarnings("unchecked")
	public void getAll() {
		init();
		listResult = new ArrayList<Tag>();
		String prefix = cachePrefix + "all";
		if (cache != null && cache.containsKey(prefix)) {
			listResult = (List<Tag>) cache.get(prefix);
			System.out.println("Get all tag from cache !");
		} else {
			int start = (page - 1) * limit;
			Query<Tag> q = ofy.query(Tag.class);
			totalResult = q.count();
			q = q.limit(limit).offset(start);
			for (Tag obj : q) {
				listResult.add(obj);
			}
			if (listResult.size() > 0) {
				cache.put(prefix, listResult);
				System.out.println("Put all tag to cache success !");
			}
		}
	}
}
